package oocl.ita.eordering.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.ResponsePo;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.util.BaseUtil;

@Controller
@RequestMapping("/homes")
public class MerchantController {
	
	@Autowired
	private MerchantService ms;
	

	//页面跳转：商户信息页面
	@RequestMapping("/info")
	public ModelAndView merchantInfo(HttpServletRequest req){
		ModelAndView mov = new ModelAndView();
//		从Session中获取当前用户信息，返回到视图加载用户信息
		Merchant m = (Merchant)req.getSession().getAttribute("user");
		mov.addObject("merchant",m);
		mov.addObject("mainPage","merInfo.jsp");
		mov.setViewName("home");
		return mov;
	}
	
	//表单提交：商户信息编辑
	@RequestMapping("editInfo")
	public ModelAndView editInfo(HttpServletRequest req,String location, String brand, MultipartFile logo){
		ModelAndView mov = new ModelAndView();
		Merchant m = (Merchant)req.getSession().getAttribute("user");
		if (!location.isEmpty()) {
			m.setmLocation(location);
		}
		if (!brand.isEmpty()) {
			m.setmBrand(brand);
		}
		if (logo.getSize()!=0) {
			String mLogoPath=ms.getLogoPath(m.getmTel(), logo);
			m.setmLogoPath(mLogoPath);
		}
		ms.updateMerchant(m);
		mov.addObject("msg",true);
		mov.addObject("merchant",m);
		mov.addObject("mainPage","merInfo.jsp");
		mov.setViewName("home");
		return mov;
		/*ResponsePo r = new ResponsePo();
		if (rs == 1) {
			req.getSession().setAttribute("user", m);
			r.setStatus(true);
		} else {
			r.setStatus(false);
		}
		return r;*/
	}
	
	//页面跳转：商户密码修改
	@RequestMapping("/linkPwd")
	public ModelAndView linkPwd(){
		ModelAndView mov = new ModelAndView();
		mov.addObject("mainPage","changePassword.jsp");
		mov.setViewName("home");
		return mov;
	}
	
	//表单提交：修改密码,返回信息页面
	@RequestMapping("/changePwd")
	@ResponseBody
	public ResponsePo changePwd(String pwd, HttpServletRequest req){
		ResponsePo rs = new ResponsePo();
		Merchant m = (Merchant)req.getSession().getAttribute("user");
		String psd = BaseUtil.JohnMd(pwd,"md5");
		m.setmPassword(psd);
		ms.updatePassword(m);
		req.getSession().setAttribute("user", m);
		rs.setStatus(true);
		return rs;
	}
	
	//修改密码：验证旧密码是否正确
	@RequestMapping("/checkPwd")
	@ResponseBody
	public ResponsePo checkPwd(String old, HttpServletRequest req){
		ResponsePo r = new ResponsePo();
		Merchant m = (Merchant)req.getSession().getAttribute("user");
		String pwd = BaseUtil.JohnMd(old,"md5");
		Merchant rs = ms.findMerchantByTel(m.getmTel());
		if(rs.getmPassword().equals(pwd)){
			r.setStatus(true);
		}else{
			r.setStatus(false);
		}
		return r;
		
	}
	
	//页面跳转：商家评分页面,显示评分
	@RequestMapping("/grade")
	public ModelAndView grade(HttpServletRequest req){
		ModelAndView mov = new ModelAndView();
		Merchant sm = (Merchant)req.getSession().getAttribute("user");
		Merchant m = ms.findMerchantById(sm.getmId());
		req.getSession().setAttribute("user", m);
		mov.addObject("star",m.getmStark());
		mov.addObject("mainPage","merGrade.jsp");
		mov.setViewName("home");
		return mov;
	}
	
	
	
}
